package tq.com.tqteacher.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.adapt.TQLookerDetailAdapter;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQUserInfo;
import tq.com.tqteacher.net.TQNetInterface;

public class TQLookerDetailActivity extends BaseTitleActivity {
    private TQLookerDetailFragment mLookerDetailFragment = new TQLookerDetailFragment();

    @Override
    protected BaseFragment fragment() {
        return mLookerDetailFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("查看明细");
    }

    static public class TQLookerDetailFragment extends BaseRecyclerFragment{
        private TQLookerDetailAdapter mLookerDetailAdapter ;
        private int page = 1;
        @Override
        protected void initRecycleView(RecyclerView pRecyclerView) {
            super.initRecycleView(pRecyclerView);

            if(mLookerDetailAdapter == null){
                mLookerDetailAdapter = new TQLookerDetailAdapter(getContext());
            }

            pRecyclerView.setAdapter(mLookerDetailAdapter);
            pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        }

        @Override
        protected void initRefresh(SmartRefreshLayout pRefreshLayout) {
            pRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                    page = 1;
                    getData();
                }
            });
            pRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                    page++;
                    getData();
                }
            });

            pRefreshLayout.autoRefresh();
        }

        private void getData(){
            TQLoadingHelper.build(getContext()).setCancelable(false).show();
            TQNetInterface.getMaterialReader(TQPublishType.getSharePublishItem().getSeq_id(), page, new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    boolean bRefresh = false;
                    if(mRefreshLayout.getState() == RefreshState.Loading){
                        mRefreshLayout.finishLoadMore();
                    }else{
                        mRefreshLayout.finishRefresh();
                        bRefresh = true;
                    }

                    if(!pNetResult.isSuccess()){
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    try {
                        if(pNetResult.getData() == null) return;

                        ArrayList<TQUserInfo> lUserInfos = new ArrayList<>();
                        JSONArray lArray = pNetResult.getData().optJSONArray("reader");
                        for (int i = 0; i < lArray.length(); i++){
                            JSONObject lObject = lArray.optJSONObject(i);
                            if(lObject == null) continue;

                            TQUserInfo lTQUserInfo = new TQUserInfo();
                            lTQUserInfo.refresh(lObject);

                            lUserInfos.add(lTQUserInfo);
                        }

                        if(bRefresh){
                            mLookerDetailAdapter.clear();
                        }
                        mLookerDetailAdapter.append(lUserInfos);

                        if(lUserInfos.size() == 0){
                            mRefreshLayout.finishLoadMoreWithNoMoreData();
                        }

                        switchEmpty(mLookerDetailAdapter.getItemCount() == 0);
                    }finally {
                        TQLoadingHelper.dimiss();
                    }
                }
            });
        }

        @Override
        protected void reload() {
            page = 1;
            getData();
        }
    }
}
